<template>
  <Container
    v-for="(item, index) in dataSource"
    :record="{
      ...item,
      $index: index
    }"
    :key="index"
  >
    <Body :body="schema.tpl" />
  </Container>
</template>

<script setup>
import { getCurrentInstance, ref } from 'vue'
import { replaceValue } from '../../common/util'
import { isString } from '@/utils/index'
import Body from '../../container/body/index.vue'
import Container from '../../container/container/index.vue'
defineOptions({ name: 'ForCom' })

const props = defineProps({
  schema: {
    type: Object,
    required: true
  }
})

const instance = getCurrentInstance()
const dataSource = ref([])
const schema = props.schema
if (!schema.items) {
  throw new Error('缺失items属性')
}
if (!schema.tpl) {
  throw new Error('缺失tpl属性')
}
const items = schema.items
if (isString(items)) {
  dataSource.value = replaceValue(items, instance)
} else {
  dataSource.value = items
}
</script>
